package main

import (
	"fmt"

	"ddl_checker/pkg/analyzer"
)

func main() {
	fmt.Println("=== pg_query_go解析器测试 ===")
	fmt.Println()

	// 创建pg_query_go分析器
	analyzer := analyzer.NewAnalyzer(analyzer.AnalyzerTypePostgreSQL, true)

	// 测试SQL语句
	testSQLs := []string{
		"CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL);",
		"SELECT * FROM users WHERE id = 1;",
		"INSERT INTO users (id, name) VALUES (2, '张三');",
		"UPDATE users SET name = '李四' WHERE id = 1;",
		"DELETE FROM users WHERE id = 1;",
		"DROP TABLE users;",
	}

	for i, sql := range testSQLs {
		fmt.Printf("=== 测试语句 %d ===\n", i+1)
		fmt.Printf("SQL: %s\n", sql)

		// 解析SQL
		stmt, err := analyzer.ParseSingle(sql)
		if err != nil {
			fmt.Printf("❌ 解析失败: %v\n", err)
			fmt.Println()
			continue
		}

		fmt.Printf("✅ 解析成功\n")
		fmt.Printf("语句类型: %s\n", stmt.StatementType)
		fmt.Printf("语法正确: %v\n", stmt.IsValid)
		fmt.Printf("关联表: %v\n", stmt.Tables)
		fmt.Printf("关联列: %v\n", stmt.Columns)
		fmt.Printf("操作: %v\n", stmt.Operations)
		fmt.Printf("原始SQL: %s\n", stmt.OriginalSQL)
		fmt.Println()
	}

	// 测试批量解析
	fmt.Println("=== 批量解析测试 ===")
	batchSQL := `CREATE TABLE test_table (id INT PRIMARY KEY, name VARCHAR(50));
INSERT INTO test_table (id, name) VALUES (1, 'test1'), (2, 'test2');
SELECT * FROM test_table;
DROP TABLE test_table;`

	statements, err := analyzer.ParseBatch(batchSQL, ";")
	if err != nil {
		fmt.Printf("❌ 批量解析失败: %v\n", err)
	} else {
		fmt.Printf("✅ 成功解析 %d 条语句\n", len(statements))
		for i, stmt := range statements {
			fmt.Printf("%d. %s\n", i+1, stmt.StatementType)
		}
	}

	// 测试语法验证
	fmt.Println("\n=== 语法验证测试 ===")
	validSQL := "SELECT * FROM users;"
	invalidSQL := "SELECT * FROM nonexistent_table WHERE syntax_error ="

	valid, validMsg := analyzer.ValidateSQL(validSQL)
	invalid, invalidMsg := analyzer.ValidateSQL(invalidSQL)

	fmt.Printf("有效SQL: %s\n", validSQL)
	fmt.Printf("验证结果: %v, 消息: %s\n", valid, validMsg)
	fmt.Printf("无效SQL: %s\n", invalidSQL)
	fmt.Printf("验证结果: %v, 消息: %s\n", invalid, invalidMsg)
}
